package com.lib.module_base.utils

import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

/**
 * @ClassName: MD5Util
 * @Description:
 * @Author: You
 */
object MD5Util {
    /**
     * MD5加密
     *
     * @param info
     * @return
     */
    fun getMD5(info: String): String {
        return try {
            val md5 = MessageDigest.getInstance("MD5")
            md5.update(info.toByteArray(charset("UTF-8")))
            val encryption = md5.digest()
            val strBuf = StringBuilder()
            for (anEncryption in encryption) {
                if (Integer.toHexString(0xff and anEncryption.toInt()).length == 1) {
                    strBuf.append("0").append(Integer.toHexString(0xff and anEncryption.toInt()))
                } else {
                    strBuf.append(Integer.toHexString(0xff and anEncryption.toInt()))
                }
            }
            strBuf.toString()
        } catch (e: NoSuchAlgorithmException) {
            ""
        } catch (e: UnsupportedEncodingException) {
            ""
        }
    }
}